﻿@{
    ViewBag.Title = "Đơn Hàng";
    Layout = "~/Views/Shared/_Layout2.cshtml";
    var user = ViewData["owner"] as SmallJob.Model.User;
    var lst = ViewData["lstmes"] as List<SmallJob.Model.MConversation>;
    var asWokerFirst = true;
    if (lst.Count > 0)
    {
        var first = lst[0];
        asWokerFirst = (first.Type == (int)SmallShop.Services.Implements.MessageType.ORDER_PROVIDE ? user.ID == first.Order.Provide.HostID : user.ID == first.Order.Request.HostID);
    }
   
}
<div class="inbox-detail">
    <div class="inbox-detail-container">
        <div class="row-fluid">
            <div class="span4 sidebar">
                <div class="inbox-detail-box sidebar-block" id="service-detail-creator">
                    <div class="container-header" id="navigate-cvs">
                        <ul class="nav nav-pills inbox-nav">
                            <li class="@(asWokerFirst ? "active" : "")" data-name="sell">
                                <a href="#" id="order-link-all">Việc cần làm</a>
                            </li>
                            <li class="@(asWokerFirst ? "" : "active")" data-name="buy">
                                <a href="#" id="provide-link-all">Dịch vụ đã đặt</a>
                            </li>
                        </ul>
                    </div>
                    <div class="conversation-list">
                        @foreach (var item in lst)
                        {
                            var isop = item.Type == (int)SmallShop.Services.Implements.MessageType.ORDER_PROVIDE;
                            var isor = item.Type == (int)SmallShop.Services.Implements.MessageType.ORDER_REQUEST;
                            var client = (user.ID == item.Person1ID ? item.User2 : item.User1);
                            var asworker = SmallShop.Services.Code.OrderHelper.TestIsWorker(item, user.ID);
                            <a class="conversation-item @(lst.IndexOf(item) == 0 ? "conversation-item-active" : "")" id="Cvs-@item.ShortID" style="@(asWokerFirst ? (asworker ? "" : "display:none;") : (asworker ? "display:none;" : ""))" onclick="loadCvs(this);" href="#">
                                <input type="hidden" class="hidden-cvs-order-type"  value="@(asworker ? 1 : 0)" />
                                <div class="conversation-item-inner">
                                    <div class="sender-avatar pull-left" title="Thăm trang cá nhân người này.">
                                        <img src="@(string.IsNullOrWhiteSpace(client.Avatar) ? Url.Content("~/Content/img/blank-avatar.jpg") : client.Avatar)?w=64&h=64&q=90&format=jpg&mode=crop" />
                                    </div>
                                    <div class="inbox-body">
                                        <p class="inbox-meta">
                                            <span class="inbox-sender">@(client.Name)</span>
                                            <span class="inbox-date">@string.Format("{0:m}", item.Message.Date)</span>
                                        </p>
                                        <p class="inbox-title">
                                            @(isor ? item.Order.Request.Title : item.Order.Provide.Title)
                                        </p>
                                    </div>
                                </div>
                            </a>
                        }
                    </div>
                    <p class="text-center" style="margin-top:10px;"><a href="javascript:void(0)" onclick="loadmoreconversation(this)" data-nextsell="@ViewData["nextpagesell"]" data-nextbuy="@ViewData["nextpagebuy"]" id="cvs-load-more">Xem thêm</a></p>
                </div>
            </div>
            @if (lst.Count > 0)
            {
                <div class="span8 inbox-detail-box main-column">
                    <div id="inbox-partial"></div>
                    @*@{if (cvs != null) { Html.RenderPartial("ConversationPV", cvs, new ViewDataDictionary { { "displayorder", ViewData["displayorder"] }}); }}*@

                    <div class="reply-box">
                        <input type="hidden" id="reply-cvs-id" value="@(lst.Count <= 0 ? "0" : lst[0].ShortID)" />
                        <form class="form-horizontal reply-form">
                            <textarea rows="3" class="span12"></textarea>
                            <button type="submit" class="btn btn-primary conversation-send" id="reply-btn">Gửi</button>
                        </form>
                    </div>
                </div>
            }
        </div>
    </div>
</div>
<script>
    var userid = '@user.ID';
    function loadCvs(that) {
        var id = $(that).attr('id').substring('Cvs-'.length);;
        $.ajax({
            type: "POST",
            url: '/Personal/OrderDetailPV',
            data: { cvsid: id },
            success: function (data) {

                $('#inbox-partial').html(data);
                $('.conversation-item-active').removeClass('conversation-item-active');
                $(that).addClass('conversation-item-active');
                $('#reply-cvs-id').val(id);

                $.post('@Url.Action("GetUnreadOrder", "Message")', function (data) {
                    if (data >= 0) {
                        $('#unread-mess-order').text(data);
                    }
                });
                $('.conversation-detail-inner').animate({ scrollTop: $('.conversation-detail-inner')[0].scrollHeight }, 1000);
            }
        });
    }

    function activeorder() {
        $('.hidden-cvs-order-type').each(function () {
            var thisId = $(this).val();
            if (thisId == 1) {
                $(this).parent().css('display', '');
            } else {
                $(this).parent().css('display', 'none');
            }
        });
        $('#provide-link-all').parent('li').removeClass('active');
        $('#order-link-all').parent('li').addClass('active');
        checkandenablecvsmore();
    }
    function activeprovide() {
        $('.hidden-cvs-order-type').each(function () {
            var thisId = $(this).val();
            if (thisId == 0) {
                $(this).parent().css('display', '');
            } else {
                $(this).parent().css('display', 'none');
            }
        });
        $('#provide-link-all').parent('li').addClass('active');
        $('#order-link-all').parent('li').removeClass('active');
        checkandenablecvsmore();
    }
    $('#order-link-all').click(function () {
        activeorder();
    });
    $('#provide-link-all').click(function () {
        activeprovide();
    });
    $('#reply-btn').click(function (e) {
        e.preventDefault();
        if ($('#reply-cvs-id').val() <= 0) return;
        $('#reply-btn').attr('disabled', 'disabled');
        var mes = $('.reply-box').find('textarea').val();
        if (mes.length > 0) {
            var preId = $('#reply-cvs-id').val();
            $.post("/Personal/SendMessage", { mes: mes, preId: preId }, function (data) {
                $('.conversation-detail-inner').append($(data).filter('.wrapper-all-message').html());
                $('.reply-box').find('textarea').val("");
                $('.conversation-detail-inner').animate({ scrollTop: $('.conversation-detail-inner')[0].scrollHeight }, 1000);
                $('#reply-btn').removeAttr('disabled');
            });
        }
    });
    $('document').ready(function () {
        var cvstoload = null;
        if (window.location.hash == "#cua-toi") {
            activeorder();
            cvstoload = $('.conversation-item').filter(':visible').first();
        }
        else if (window.location.hash == "#toi-dat") {
            activeprovide();
            cvstoload = $('.conversation-item').filter(':visible').first();
        } else {
            cvstoload = $('#Cvs-' + window.location.hash.substring(1));
            if (cvstoload != null && cvstoload.length !== 0) {
                var typecvs = $(cvstoload).find('.hidden-cvs-order-type');
                var type = $(typecvs).val();
                console.log(type);
                if (type == 1) {
                    activeorder();
                } else {
                    activeprovide();
                }
            }
        }
        if (cvstoload == null || cvstoload.length === 0) {
            cvstoload = $('.conversation-item').filter(':visible').first();
        }
        loadCvs(cvstoload);
        checkandenablecvsmore();
        //$('.conversation-detail-inner').animate({ scrollTop: $('.conversation-detail-inner')[0].scrollHeight }, 1000);
    });
    function makedoneorder(that) {
        var parent = $(that).parent();
        var isok = confirm("Bạn có chắc muốn hoàn thành order này.");
        if (!isok) return;
        var id = $('.conversation-item-active').attr('id').substring('Cvs-'.length);;
        var orderid = $('#order-hidden-' + id).val();
        $.post("/Personal/MakeDoneOrder", { orderid: id }, function (data) {
            if (data.result == true) {
                window.location.reload();
            }
            else {
                alert("error");
            }
        });
    }
    function makecancelorder(that) {
        var parent = $(that).parent();
        var isok = confirm("Bạn có chắc muốn hủy order này.");
        if (!isok) return;
        var id = $('.conversation-item-active').attr('id').substring('Cvs-'.length);;
        var orderid = $('#order-hidden-' + id).val();
        $.post("/Personal/MakeCancelOrder", { orderid: id }, function (data) {
            if (data.result == true) {
                window.location.reload();
            }
            else {
                alert("error");
            }
        });
    }
    function commitcomment(that) {
        var parent = $(that).parent();
        var comment = $(parent).find('textarea').val();
        var isok = confirm("Bạn đã nhận xét xong?");
        if (!isok) return;
        var id = $('.conversation-item-active').attr('id').substring('Cvs-'.length);;
        var orderid = $('#order-hidden-' + id).val();
        if (comment.length <= 0) return;
        else {
            $.post("/Personal/CommentOrder", { orderid: orderid, comment: comment }, function (data) {
                if (data.result) {
                    window.location.reload();
                }
                else {
                    alert("error");
                }
            });
        }
    }
    function commitrate(that) {
        var parent = $(that).parent();
        var rate = $('#rateit5').rateit('value');
        var isok = confirm("Bạn đã đánh giá xong?");
        if (!isok) return;
        var id = $('.conversation-item-active').attr('id').substring('Cvs-'.length);
        var orderid = $('#order-hidden-' + id).val();
        if (rate <= 0) {
            alert("Bạn chưa đánh giá.");
            return;
        }
        else {

            $.post("/Personal/RateOrder", { orderid: orderid, rate: rate }, function (data) {
                if (data.result == true) {
                    var toload = $('.conversation-item-active');
                    loadCvs(toload);
                }
                else {
                    alert("error");
                }
            });
        }
    }
    function loadmoremessage(that) {
        if (isdisablemore(that)) {
            return;
        }
        disablemore(that);
        var nextpage = $(that).attr('data-nextpage');
        var cvsid = $('#reply-cvs-id').val();
        $.ajax({
            type: "POST",
            url: '/Personal/ConversationPV',
            data: { pageforload: nextpage, cvsId: cvsid },
            success: function (data) {
                var pagemore = $(data).filter('#new-next-page');
                if (pagemore.length !== 0) {
                    if (!$('#get-next-page').is(":visible")) {
                        $('#get-next-page').show();
                    }
                    $('#get-next-page').attr('data-nextpage', $(pagemore).attr('data-nextpage'));
                } else {
                    $('#get-next-page').hide();
                }
                $('.conversation-detail-inner').prepend($(data).filter('.wrapper-all-message').html());

                $.post('@Url.Action("GetUnreadOrder", "Message")', function (data) {
                    if (data >= 0) {
                        $('#unread-mess-order').text(data);
                    }
                });
                enablemore(that);
            }
        });
    }
    function loadmoreconversation(that) {
        if (isdisablemore(that)) {
            return;
        }
        disablemore(that);
        var liactive = $('#navigate-cvs').find('li.active');
        var curactive = $(liactive).attr('data-name');
        if (curactive == "sell") {
            var nextpage = $(that).attr('data-nextsell');
            $.post("/Personal/LoadMoreCvsOrderPV", { isordersell: true, page: nextpage }, function (data) {
                var newcvs = $(data).filter('.wrapper-conversations').html();
                $('.conversation-list').append(newcvs);
                var pagemore = $(data).filter('#new-next-page');
                if (pagemore.length !== 0) {
                    $(that).attr('data-nextsell', $(pagemore).attr('data-nextpage'));
                } else {
                    $(that).attr('data-nextsell', 0);
                }
                enablemore(that);
                checkandenablecvsmore();
            });
        }
        if (curactive == "buy") {
            var nextpage = $(that).attr('data-nextbuy');
            $.post("/Personal/LoadMoreCvsOrderPV", { isordersell: false, page: nextpage }, function (data) {
                var newcvs = $(data).filter('.wrapper-conversations').html();
                $('.conversation-list').append(newcvs);
                var pagemore = $(data).filter('#new-next-page');
                if (pagemore.length !== 0) {
                    $(that).attr('data-nextbuy', $(pagemore).attr('data-nextpage'));
                } else {
                    $(that).attr('data-nextbuy', 0);
                }
                enablemore(that);
                checkandenablecvsmore();
            });
        }
    }
    function checkandenablecvsmore() {
        var liactive = $('#navigate-cvs').find('li.active');
        var curactive = $(liactive).attr('data-name');
        if (curactive == "sell") {
            var nextpage = $('#cvs-load-more').attr('data-nextsell');
            if (nextpage == 0) {
                $('#cvs-load-more').hide();
            } else {
                $('#cvs-load-more').show();
            }
        }
        if (curactive == "buy") {
            var nextpage = $('#cvs-load-more').attr('data-nextbuy');
            if (nextpage == 0) {
                $('#cvs-load-more').hide();
            } else {
                $('#cvs-load-more').show();
            }
        }
    }
    function disablemore(that) {
        $(that).addClass('moredisable');
    }
    function enablemore(that) {
        $(that).removeClass('moredisable');
    }
    function isdisablemore(that) {
        return $(that).hasClass('moredisable');
    }
</script>
